home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTPREV.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  109 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btprev.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "btree_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btprev - previous btree key
  20.  
  21. SYNOPSIS
  22.      #include <btree.h>
  23.  
  24.      int btprev(btp)
  25.      btree_t *btp;
  26.  
  27. DESCRIPTION
  28.      The btprev function retreats the cursor of btree btp to the
  29.      previous key.  If cursor is currently null, it will be moved to
  30.      the last key.  If the cursor is currently on the last key, it
  31.      will move to null.  If the tree is empty, the cursor will remain
  32.      on null.
  33.  
  34.      btprev will fail if one or more of the following is true:
  35.  
  36.      [EINVAL]       btp is not a valid btree pointer.
  37.      [BTELOCK]      btp is not locked.
  38.      [BTENOPEN]     btp is not open.
  39.  
  40. SEE ALSO
  41.      btcursor, btfirst, btlast, btnext.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise,
  45.      a value of -1 is returned, and errno set to indicate the error.
  46.  
  47. ------------------------------------------------------------------------------*/
  48. #ifdef AC_PROTO
  49. int btprev(btree_t *btp)
  50. #else
  51. int btprev(btp)
  52. btree_t *btp;
  53. #endif
  54. {
  55.     int terrno = 0;        /* tmp errno */
  56.  
  57.     /* validate arguments */
  58.     if (!bt_valid(btp)) {
  59.         errno = EINVAL;
  60.         return -1;
  61.     }
  62.  
  63.     /* check if not open */
  64.     if (!(btp->flags & BTOPEN)) {
  65.         errno = BTENOPEN;
  66.         return -1;
  67.     }
  68.  
  69.     /* check locks */
  70.     if (!(btp->flags & BTLOCKS)) {
  71.         errno = BTELOCK;
  72.         return -1;
  73.     }
  74.  
  75.     /* move cursor to previous key in current node */
  76.     if (btp->cbtpos.node != NIL) {
  77.         if (--btp->cbtpos.key > 0) {
  78.             return 0;
  79.         }
  80.     }
  81.  
  82.     /* move cursor to null */
  83.     if (btp->cbtpos.node == btp->bthdr.first) {
  84.         btp->cbtpos.node = NIL;
  85.         btp->cbtpos.key = 0;
  86.         bt_ndinit(btp, btp->cbtnp);
  87.         return 0;
  88.     }
  89.  
  90.     /* move cursor to last key in prev node */
  91.     if (btp->cbtpos.node == NIL) {
  92.         btp->cbtpos.node = btp->bthdr.last;
  93.     } else {
  94.         btp->cbtpos.node = btp->cbtnp->lsib;
  95.     }
  96.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  97.         BTEPRINT;
  98.         terrno = errno;
  99.         btp->cbtpos.node = NIL;
  100.         btp->cbtpos.key = 0;
  101.         bt_ndinit(btp, btp->cbtnp);
  102.         errno = terrno;
  103.         return -1;
  104.     }
  105.     btp->cbtpos.key = btp->cbtnp->n;
  106.  
  107.     return 0;
  108. }
  109.